package com.jiahe.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiahe.blog.common.exception.BlogException;
import com.jiahe.blog.dao.RoleDao;
import com.jiahe.blog.dao.UserAuthDao;
import com.jiahe.blog.dao.UserInfoDao;
import com.jiahe.blog.entity.UserAuthEntity;
import com.jiahe.blog.entity.UserInfoEntity;
import com.jiahe.blog.model.BlogConst;
import com.jiahe.blog.utils.StringUtils;
import com.jiahe.blog.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import static com.jiahe.blog.model.RedisPrefixConst.ARTICLE_USER_LIKE;
import static com.jiahe.blog.model.RedisPrefixConst.COMMENT_USER_LIKE;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserAuthDao userAuthDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)){
            throw new BlogException("用户名不能为空！");
        }
        // 查询账号是否存在
        UserAuthEntity userAuthEntity = userAuthDao.selectOne(new LambdaQueryWrapper<UserAuthEntity>()
                .select(UserAuthEntity::getId, UserAuthEntity::getUserInfoId, UserAuthEntity::getUsername, UserAuthEntity::getPassword, UserAuthEntity::getLoginType, UserAuthEntity::getDisable)
                .eq(UserAuthEntity::getUsername, username));
        if (Objects.isNull(userAuthEntity)) {
            throw new BlogException("用户名不存在");
        }
        if (BlogConst.TURE == userAuthEntity.getDisable()) {
            throw new BlogException("用户已被禁用");
        }
        // 查询账号信息
        UserInfoEntity userInfoEntity = userInfoDao.selectOne(new LambdaQueryWrapper<UserInfoEntity>()
                .select(UserInfoEntity::getId, UserInfoEntity::getEmail, UserInfoEntity::getNickname, UserInfoEntity::getAvatar, UserInfoEntity::getIntro, UserInfoEntity::getWebSite, UserInfoEntity::getMute)
                .eq(UserInfoEntity::getId, userAuthEntity.getUserInfoId()));
        // 查询账号角色
        List<String> listRole = roleDao.listRolesByUserInfoId(userInfoEntity.getId());
        // 查询账号点赞信息
        Set<Integer> articleLikeSet = (Set<Integer>) redisTemplate.boundHashOps(ARTICLE_USER_LIKE).get(userInfoEntity.getId().toString());
        Set<Integer> commentLikeSet = (Set<Integer>) redisTemplate.boundHashOps(COMMENT_USER_LIKE).get(userInfoEntity.getId().toString());
        // 封装登录用户信息
        return UserUtils.convertLoginUser(userAuthEntity, userInfoEntity, listRole, articleLikeSet, commentLikeSet);
    }
}
